<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use app\Service\Algorithm;
use shishao\huobiApi\Huobi\HuobiApi;
class ExecTranPodcast implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $list;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($list)
    {
        $this->list=$list;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Algorithm $algorithm,HuobiApi $api)
    {
        foreach ($this->list as $v){
            $res=$algorithm::compute($v);
            if($v->trade_count==0 && $res[0]==1){
               $order=$api->place_order($v->acc_id,$v->balance,$res[1],strtolower($v->coin_name).'usdt','sell-market');
               if(!empty($order->data)){
                   $this->order_log([
                       'user_id'=>$v->user_id,
                       'ex_id'=>$v->ex_id,
                       'account_id'=>$v->account_id,
                       'curr'=>$v->coin_name,
                       'num'=>$v->balance,
                       'type'=>1,
                       'price'=>$res[1],
                       'aisle'=>$res[2]
                   ]);
               }

            }else if($v->trade_count>0){
                if($res[0]==1){
                    $order=$api->place_order($v->acc_id,$v->balance,$res[1],strtolower($v->coin_name).'usdt','sell-market');
                    if(!empty($order->data)){
                        $this->order_log([
                            'user_id'=>$v->user_id,
                            'ex_id'=>$v->ex_id,
                            'account_id'=>$v->account_id,
                            'curr'=>$v->coin_name,
                            'num'=>$v->balance,
                            'type'=>1,
                            'price'=>$res[1],
                            'aisle'=>$res[2]
                        ]);
                    }
                }else if($res[0]==2){
                    $order=$api->place_order($v->acc_id,$v->balance,$res[1],strtolower($v->coin_name).'usdt','buy-market');
                    if(!empty($order->data)){
                        $this->order_log([
                            'user_id'=>$v->user_id,
                            'ex_id'=>$v->ex_id,
                            'account_id'=>$v->account_id,
                            'curr'=>$v->coin_name,
                            'num'=>$v->balance,
                            'type'=>0,
                            'price'=>$res[1],
                            'aisle'=>$res[2]
                        ]);
                    }
                }

            }
        }
    }


    //写入订单日志
    public function order_log(array $data)
    {
        DB::table('order_log')->insert($data);
    }
}
